(function ($) {
    display("role,conf[PUBLIC_STATUS]"); // 需要翻译的类型
    $(function () {
        var oTable = $('#datatable').dataTable({
            bProcessing: true,
            "sAjaxSource": window.basePath + "/user/list.html",
            "fnServerParams": function (aoData) {
                var search = $('#searchForm')._formToJson();
                for (var i in search) {
                    aoData.push({"name": i, "value": search[i]});
                }
            },
            "aoColumnDefs": [
                {"aTargets": [0], "mData": "username"},
                {"aTargets": [1], "mData": "nick"},
                {
                    "aTargets": [2], "mData": "roleId", "mRender": function (data, type, full) {
                    return data != null ? $.fn.display.role[data] : data;
                }
                },
                {"aTargets": [3], "mData": "phone"},
                {"aTargets": [4], "mData": "qq"},
                {"aTargets": [5], "mData": "email"},
                {"aTargets": [6], "mData": "loginNum"},
                {"aTargets": [7], "mData": "lastLogin"},
                {
                    "aTargets": [8],
                    "width": "12%",
                    "mData": null,
                    mRender: function (data, type, full) {
                        var btnstr = '';
                        btnstr += '<div class="btn-group"><a class="btn btn-info btn-xs upd" href="javascript:void(0);" dataid="' + full.userId + '">修改</a>';
                        if (full.status == 1) {
                            btnstr += '<a class="btn btn-danger btn-xs disable" href="javascript:void(0);" dataid="' + full.userId + '">停用</a>';
                        } else {
                            btnstr += '<a class="btn btn-success btn-xs enable" href="javascript:void(0);" dataid="' + full.userId + '">启用</a>';
                        }
                        btnstr += '<a class="btn btn-default btn-xs resetpwd" href="javascript:void(0);" title="密码重置" uname="' + full.username + '" dataid="' + full.userId + '">重置</a>';
              //          if (full.sub_id == null && full.roleId == 5) btnstr += admin;
                        return btnstr + "</div>";
                    }
                }
            ]
        }).on('click', 'a.admin', function () {//设置分站
            alert('aaaa');
        });

        /* 查询 */
        $("#searchButton").click(function () {
            $('#datatable').dataTable()._fnAjaxReload();
        });

        /* 新增 */
        $('#newUser').click(function () {
            $('#userEditForm').attr('action', window.basePath + '/user/add.html').resetForm();
            $('#username').removeAttr('readonly');
            $.modalDialog({
                id: 'userEditDialog',
                title: '新增用户',
                ok: function () {
                    $('#userEditForm').submit();
                }
            });
        });

        /* 修改 */
        $('#datatable').on('click', 'a.upd', function (e) {
            $('#username').attr('readonly', 'readonly');
            var tr = $(this).parents('tr:first');
            var aData = oTable.fnGetData(tr[0]);
            $("#originalEmail").val(aData.email);
            $('#userEditForm').attr('action', window.basePath + '/user/upd.html').resetForm()._jsonToForm(aData);
            $.modalDialog({
                id: 'userEditDialog',
                title: '修改用户',
                ok: function () {
                    $('#userEditForm').submit();
                }
            });
        });

        /* 停用 */
        $('#datatable').on('click', 'a.disable', function (e) {
            var id = $(this).attr("dataid");
            $.modalConfirm({
                id: 'modal-confirm',
                content: '确认要停用该用户吗？',
                ok: function () {
                    $._ajaxPost(window.basePath + '/user/disable.html', {id: id}, function (r) {
                        if (r.r) {
                            $("#searchButton").click();
                        }
                        asyncbox.tips(r.m, r.r ? 'success' : 'error');
                    });
                }
            });
        });

        /* 启用 */
        $('#datatable').on('click', 'a.enable', function (e) {
            var id = $(this).attr("dataid");
            $.modalConfirm({
                id: 'modal-confirm',
                content: '确认要启用该用户吗？',
                ok: function () {
                    $._ajaxPost(window.basePath + '/user/enable.html', {id: id}, function (r) {
                        if (r.r) {
                            $("#searchButton").click();
                        }
                        asyncbox.tips(r.m, r.r ? 'success' : 'error');
                    });
                }
            });
        });

        /* reset */
        $('#datatable').on('click', 'a.resetpwd', function (e) {
            var id = $(this).attr("dataid");
            $.modalConfirm({
                id: 'modal-confirm',
                content: '确认重置用户<b>[ ' + $(this).attr("uname") + ' ]</b>的密码为<b>[ 123456 ]</b>吗？',
                ok: function () {
                    $._ajaxPost(window.basePath + '/user/resetpwd.html', {uid: id}, function (r) {
                        asyncbox.tips(r.m, r.r ? 'success' : 'error');
                    });
                }
            });
        });

        /* 参数验证 */
        $('#userEditForm').validator({
            theme: 'yellow_right_effect',
            stopOnError: false,
            timely: 1,
            ignore: ':hidden',
            fields: {
                'username': {
                    rule: '用户名:required;username;',
                    msg: {
                        required: "用户名不能为空",
                        remote: "用户名已存在",
                    },
                },
                'nick': '昵称:required;length[2~30]',
                'password': '密码:required;length[2~30]',
                'qq': 'QQ号码:digits;length[5~10]',
                'phone': '手机号码:digits;mobile',
                'email': '电子邮箱:length[2~30];email;remote[/user_check_email.html, originalEmail]',
                'roleId': '角色:required;'
            },
            valid: function () {
                submitForm();
            }
        });

        function submitForm() {
            $('#userEditForm')._ajaxForm(function (r) {
                asyncbox.tips(r.m, r.r ? 'success' : 'error');
                if (r.r) {
                    $("#searchButton").click();
                    $("#closeBtn").click()
                } else {
                    return false;
                }
            });
        }

        /**
         * 角色监听
         */
        var roleId = $('select#roleId');
        roleId.on('change', function () {
            $('select#subId').val("");
            if (roleId.val() == 5){
                $('div#sub_id_div').show();
            }else{
                $('div#sub_id_div').hide();
            }
        });
    });
})(jQuery);
var reload = function () {
    $("#searchButton").click();
};